【例子介绍】jenkins up and running(Jenkins 2权威指南)
【相关图片】
【源码结构】
Table of Contents
Foreword. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
1. Introducing Jenkins 2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
What Is Jenkins 2? 2
The Jenkinsfile 3
Declarative Pipelines 4
Blue Ocean Interface 6
New Job Types in Jenkins 2 7
Reasons for the Shift 10
DevOps Movement 10
Assembling Pipelines 11
Resumability 11
Configurability 11
Sharing Workspaces 12
Specialized Knowledge 12
Access to Logic 12
Pipeline Source Management 12
Competition 13
Meeting the Challenges 13
Compatibility 13
Pipeline Compatibility 14
Plugin Compatibility 15
Checking Compatibility 20
Summary 21
vii
2. The Foundations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Syntax: Scripted Pipelines Versus Declarative Pipelines 24
Choosing Between Scripted and Declarative Syntax 25
Systems: Masters, Nodes, Agents, and Executors 26
Master 27
Node 27
Agent 27
Executor 28
Creating Nodes 29
Structure: Working with the Jenkins DSL 31
node 32
stage 34
steps 35
Supporting Environment: Developing a Pipeline Script 36
Starting a Pipeline Project 37
The Editor 38
Working with the Snippet Generator 40
Running a Pipeline 45
Replay 52
Summary 56
3. Pipeline Execution Flow. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Triggering Jobs 60
Build After Other Projects Are Built 60
Build Periodically 61
GitHub Hook Trigger for GitSCM Polling 64
Poll SCM 64
Quiet Period 64
Trigger Builds Remotely 64
User Input 65
input 65
Parameters 69
Return Values from Multiple Input Parameters 75
Parameters and Declarative Pipelines 76
Flow Control Options 81
timeout 81
retry 83
sleep 83
waitUntil 83
Dealing with Concurrency 85
Locking Resources with the lock Step 86
Controlling Concurrent Builds with Milestones 87
viii | Table of Contents
Restricting Concurrency in Multibranch Pipelines 89
Running Tasks in Parallel 89
Conditional Execution 99
Post-Processing 100
Scripted Pipelines Post-Processing 101
Declarative Pipelines and Post-Processing 103
Summary 104
4. Notifications and Reports. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Notifications 105
Email 106
Collaboration Services 116
Reports 128
Publishing HTML Reports 129
Summary 131
5. Access and Security. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Securing Jenkins 133
Enabling Security 134
Other Global Security Settings 137
Credentials in Jenkins 142
Credential Scopes 143
Credential Domains 144
Credential Providers 144
Credential Stores 144
Administering Credentials 145
Selecting Credential Providers 145
Selecting Credential Types 146
Specifying Credential Types by Provider 146
Creating and Managing Credentials 147
Context Links 150
Adding a New Domain and Credential 151
Using the New Domain and Credential 153
Advanced Credentials: Role-Based Access 155
Basic Use 155
Manage Roles 157
Assign Roles 162
Role Strategy Macros 166
Working with Credentials in the Pipeline 168
Username and Password 168
SSH Keys 168
Token Credentials 169
Table of Contents | ix
Controlling Script Security 170
Script Checking 171
Script Approval 172
Groovy Sandboxing 173
Using Jenkins Credentials with Vault 175
Approach 176
Setup 176
Creating a Policy 176
Authentication 177
Using Vault in Jenkins 179
Summary 182
6. Extending Your Pipeline. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
Trusted Versus Untrusted Libraries 183
Internal Versus External Libraries 184
Internal Libraries 184
External Libraries 187
Getting a Library from the Source Repository 188
Modern SCM 188
Legacy SCM 188
Using Libraries in Your Pipeline Script 189
Automatic Downloading of Libraries from Source Control 190
Loading Libraries into Your Script 190
Library Scope Within Jenkins Items 193
Library Structure 193
Sample Library Routine 194
Structure of Shared Library Code 195
Using Third-Party Libraries 205
Loading Code Directly 206
Loading Code from an External SCM 207
Replaying External Code and Libraries 208
A Closer Look at Trusted Versus Untrusted Code 211
Summary 214
7. Declarative Pipelines. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Motivation 216
Not Intuitive 216
Getting Groovy 216
Additional Assembly Required 217
The Structure 218
Block 218
Section 218
x | Table of Contents
Directives 219
Steps 219
Conditionals 220
The Building Blocks 220
pipeline 221
agent 222
environment 225
tools 226
options 229
triggers 232
parameters 234
libraries 237
stages 238
post 241
Dealing with Nondeclarative Code 242
Check Your Plugins 243
Create a Shared Library 243
Place Code Outside of the Pipeline Block 243
The script Statement 244
Using parallel in a Stage 244
Script Checking and Error Reporting 245
Declarative Pipelines and the Blue Ocean Interface 248
Summary 248
8. Understanding Project Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
Common Project Options 251
General 251
Source Code Management 258
Build Triggers 259
Build Environment 267
Build 277
Post-Build Actions 277
Types of Projects 278
Freestyle Projects 278
The Maven Project Type 279
The Pipeline Project Type 282
The External Job Project Type 284
The Multiconfiguration Project Type 287
Ivy Projects 293
Folders 295
Multibranch Pipeline Projects 300
GitHub Organization Projects 305
Table of Contents | xi
Bitbucket Team/Project Projects 310
Summary 313
9. The Blue Ocean Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
Part 1: Managing Existing Pipelines 316
The Dashboard 316
The Project-Specific Page 321
The Run Page 332
Part 2: Working with the Blue Ocean Editor 342
Creating a New Pipeline Without an Existing Jenkinsfile 342
Working in the Editor 347
Editing an Existing Pipeline 359
Importing and Editing Existing Pipelines 363
Working with Pipelines from Non-GitHub Repositories 375
Summary 377
10. Conversions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
Common Preparation 380
Logic and Accuracy 380
Project Type 380
Systems 380
Access 381
Global Configuration 381
Plugins 381
Shared Libraries 382
Converting a Freestyle Pipeline to a Scripted Pipeline 382
Source 386
Compile 391
Unit Tests 396
Integration Testing 400
Migrating the Next Parts of the Pipeline 403
Converting from a Jenkins Pipeline Project to a Jenkinsfile 409
Approach 412
Final Steps 418
Converting from a Scripted Pipeline to a Declarative Pipeline 420
Sample Pipeline 421
The Conversion 422
Completed Conversion 426
General Guidance for Conversions 427
Summary 429
xii | Table of Contents
11. Integration with the OS (Shells, Workspaces, Environments, and Files). . . . . . . . . . . . 431
Using Shell Steps 432
The sh Step 432
The bat Step 437
The powershell Step 438
Working with Environment Variables 440
The withEnv Step 441
Working with Workspaces 442
Creating a Custom Workspace 442
Cleaning a Workspace 444
File and Directory Steps 446
Working with Files 446
Working with Directories 448
Doing More with Files and Directories 449
Summary 450
12. Integrating Analysis Tools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
SonarQube Survey 454
Working with Individual Rules 455
Quality Gates and Profiles 459
The Scanner 461
Using SonarQube with Jenkins 462
Global Configuration 462
Using SonarQube in a Freestyle Project 463
Using SonarQube in a Pipeline Project 464
Leveraging the Outcome of the SonarQube Analysis 465
SonarQube Integration Output with Jenkins 469
Code Coverage: Integration with JaCoCo 470
About JaCoCo 470
Integrating JaCoCo with the Pipeline 471
JaCoCo Output Integration with Jenkins 473
Summary 474
13. Integrating Artifact Management. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
Publishing and Retrieving Artifacts 477
Setup and Global Configuration 478
Using Artifactory in a Scripted Pipeline 479
Performing Other Tasks 484
Downloading Specific Files to Specific Locations 484
Uploading Specific Files to Specific Locations 485
Setting Build Retention Policies 485
Build Promotion 485
Table of Contents | xiii
Integration with a Declarative Pipeline 486
Artifactory Integration with Jenkins Output 486
Archiving Artifacts and Fingerprinting 487
Summary 493
14. Integrating Containers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
Configured as a Cloud 497
Global Configuration 498
Using Docker Images as Agents 501
Using Cloud Images in a Pipeline 505
Agent Created on the Fly for a Declarative Pipeline 509
Docker Pipeline Global Variable 512
Global Variables 512
Docker Application Global Variable Methods 514
Docker Image Global Variable Methods 519
Docker Container Global Variable Methods 524
Running Docker via the Shell 524
Summary 526
15. Other Interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527
Using the Command-Line Interface 528
Using the Direct SSH Interface 528
Using the CLI Client 531
Using the Jenkins REST API 534
Filtering Results 534
Initiating Builds 536
Using the Script Console 538
Summary 541
16. Troubleshooting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543
Diving into Pipeline Steps 543
Dealing with Serialization Errors 547
Continuous Passing Style 547
Serializing Pipelines 547
NotSerializableException 548
Handling Nonserializable Errors 549
Identifying the Line in Your Script that Caused an Error 551
Handling Exceptions in a Pipeline 552
Using Nondeclarative Code Within a Declarative Pipeline 553
Unapproved Code (Script and Method Approval) 556
Unsupported Operations 557
System Logs 558
xiv | Table of Contents
Timestamps 559
Pipeline Durability Settings 560
Summary 562
Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563
评论